﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.DrivenRectTransformTracker
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine.Bindings;

#nullable disable
namespace UnityEngine
{
  /// <summary>
  ///   <para>A component can be designed to drive a RectTransform. The DrivenRectTransformTracker struct is used to specify which RectTransforms it is driving.</para>
  /// </summary>
  [NativeHeader("Editor/Src/Animation/AnimationModeSnapshot.h")]
  [NativeHeader("Editor/Src/Undo/PropertyUndoManager.h")]
  public struct DrivenRectTransformTracker
  {
    private List<RectTransform> m_Tracked;
    private static bool s_BlockUndo;

    internal static bool CanRecordModifications()
    {
      return !DrivenRectTransformTracker.IsInAnimationMode() && (DrivenRectTransformTracker.IsUndoingOrRedoing() || DrivenRectTransformTracker.HasUndoRecordObjects());
    }

    [FreeFunction("GetAnimationModeSnapshot().IsInAnimationMode")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern bool IsInAnimationMode();

    [FreeFunction("GetPropertyUndoManager().HasRecordings")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern bool HasUndoRecordObjects();

    [FreeFunction("GetPropertyUndoManager().IsUndoingOrRedoing")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern bool IsUndoingOrRedoing();

    /// <summary>
    ///   <para>Stop recording undo actions from driven RectTransforms.</para>
    /// </summary>
    public static void StopRecordingUndo() => DrivenRectTransformTracker.s_BlockUndo = true;

    /// <summary>
    ///   <para>Resume recording undo of driven RectTransforms.</para>
    /// </summary>
    public static void StartRecordingUndo() => DrivenRectTransformTracker.s_BlockUndo = false;

    /// <summary>
    ///   <para>Add a RectTransform to be driven.</para>
    /// </summary>
    /// <param name="driver">The object to drive properties.</param>
    /// <param name="rectTransform">The RectTransform to be driven.</param>
    /// <param name="drivenProperties">The properties to be driven.</param>
    public void Add(
      Object driver,
      RectTransform rectTransform,
      DrivenTransformProperties drivenProperties)
    {
      if (this.m_Tracked == null)
        this.m_Tracked = new List<RectTransform>();
      if (!Application.isPlaying && DrivenRectTransformTracker.CanRecordModifications() && !DrivenRectTransformTracker.s_BlockUndo)
        RuntimeUndo.RecordObject((Object) rectTransform, "Driving RectTransform");
      if (rectTransform.drivenByObject != driver)
        rectTransform.drivenProperties = DrivenTransformProperties.None;
      rectTransform.drivenByObject = driver;
      rectTransform.drivenProperties |= drivenProperties;
      this.m_Tracked.Add(rectTransform);
    }

    [Obsolete("revertValues parameter is ignored. Please use Clear() instead.")]
    public void Clear(bool revertValues) => this.Clear();

    /// <summary>
    ///   <para>Clear the list of RectTransforms being driven.</para>
    /// </summary>
    public void Clear()
    {
      if (this.m_Tracked == null)
        return;
      for (int index = 0; index < this.m_Tracked.Count; ++index)
      {
        if ((Object) this.m_Tracked[index] != (Object) null)
        {
          if (!Application.isPlaying && DrivenRectTransformTracker.CanRecordModifications() && !DrivenRectTransformTracker.s_BlockUndo)
            RuntimeUndo.RecordObject((Object) this.m_Tracked[index], "Driving RectTransform");
          this.m_Tracked[index].drivenByObject = (Object) null;
          this.m_Tracked[index].drivenProperties = DrivenTransformProperties.None;
        }
      }
      this.m_Tracked.Clear();
    }
  }
}
